<?php
namespace Admin\Controller;
use Common\Controller\BackendController;
class IndexController extends BackendController{
    public function _initialize(){
        parent::_initialize();
        $this->_mod=D('Menu');
    }
    public  function index(){
        session('error_login_count_admin',0);
        if(false=== $menus= F("admin_menu/{$_SESSION['admin']['role_id']}/sub_menu_0")){
          $menus=$this->_mod->sub_menu_cache(0);  
        } 
       
        $this->assign('menus',$menus);
        $this->display();
    }
    public function login(){
      
        if(IS_POST){
          $username=I('post.username','','trim');
          $password=I('post.password','','trim');
          $code=I('post.code','','trim');//后面加入
          !$username && $err='用户名不能为空!';
          if(!$err && !$password) $err='密码不能为空!';
          $admin=M('admin')->field('id,username,password,pwd_hash,role_id')->where(array('username'=>$username))->find();
          if(!$err && !$admin) $err='管理员账号不存在!';
          if(!$err && $admin['password']!=md5(md5($password).$admin['pwd_hash'].C('PWDHASH'))) $err='用户名或者密码不正确';
          //通过验证存储到session
          if(!$err){
            $_SESSION['admin']=array(
                 'id'=>$admin['id'],
                 'role_id'=>$admin['role_id'], 
                 'username'=>$admin['username'],  
                  ); 
          //记录管理员登录
          M('admin')->where(array('id'=>$admin['id']))->save(array('last_login_time'=>time(),'last_login_ip'=>get_client_ip()));
         //删除后台管理员3个月之内的日志
          M("admin_log")->where(array('add_time'=>array('lt',strtotime("-90 days"))))->delete();
          $this->redirect('index/index');
        } 
        $this->assign('err',$err);
       }
       $this->display(); 
    }
    
    //顶部菜单
    public function top_menu(){
        if(false=== $menus = F("admin_menu/{$_SESSION['admin']['role_id']}/sub_menu_0")){
            $menus=$this->_mod->sub_menu_cache(0);
        }
        $this->assign('menus',$menus['menu']);
        $this->display();
    }
    //左侧菜单
   public function left_menu(){
       $menuid=I('get.menuid',0,intval);
       if($menuid){
           if(false=== $menus=F("admin_menu/{$_SESSION['admin']['role_id']}/sub_menu_{$menuid}")){
              $menus=$this->_mod->sub_menu_cache($menuid); 
           }
           $menus=$menus['menu'];
       }else{
           //快捷菜单
           if (false===F("console_menu")){
               $menus=$this->_mod->console_cache();
           }
       }
       $this->assign('menuid',$menuid);
       $this->assign('menus',$menus);
       $this->display();
   }
    
    
}